package com.evil.kingdom.pojo.enums;

import com.evil.common.core.exception.BusinessException;
import com.evil.kingdom.pojo.enums.result.RCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;

/**
 * 人物类型
 *
 * @author liyang
 * @date 2024-05-17 17:46
 */
@Getter
@AllArgsConstructor
public enum PersonTypeEnum {

    KING(0, "君主"),
    GENERAL(1, "将军"),
    SOLDIER(2, "士兵");

    private final int type;
    private final String name;

    public static Optional<PersonTypeEnum> isExistByType(Integer type) {
        if (null == type) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.type == type).findFirst();
    }

    public static PersonTypeEnum findByType(Integer type) {
        return PersonTypeEnum.isExistByType(type).orElseThrow(PersonTypeEnum::error);
    }

    public static BusinessException error() {
        return new BusinessException(RCodeEnum.ERROR_PARAMETER, "人物类型错误");
    }
}
